package com.server.service;

import com.server.Untils.MybatisClass;
import com.server.Untils.ResponseMessage;
import com.server.entity.Users;
import com.server.example.UsersExample;
import com.server.mapper.UsersMapper;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Admin
 */
public class UsersService {

    /**
     * 用户登录功能实现
     *<br/>
     * 执行流程:
     * - 传递用户的账号与密码
     * - 账号存在
     *    - 账号的密码是否正确，并在数据库中记录登录信息
     *      - 正确，返回用户id
     *      - 错误，返回null
     * - 账号不存在
     *    - 账号信息未找到
     *
     * 调用方法：Users usernamesLogin = new UsersService().userLogin(new Users("admin","admin"));     *
     * @param userLogin 用户登录
     * @return 用户登录的数据
     */
    public Users userLogin(Users userLogin){
        SqlSession sqlSession = MybatisClass.getSqlSession();
        Users userinfo = null;
        UsersMapper usersMapper = sqlSession.getMapper(UsersMapper.class);
        UsersExample usersExample = new UsersExample();
        usersExample.createCriteria().andUserNameEqualTo(userLogin.getUserName());
        try {
            Users serifs = usersMapper.selectByExample(usersExample).get(0);
            if (serifs.getUserPassword().equals(userLogin.getUserPassword())){
                /*打印登录成功的消息*/
                System.out.println(ResponseMessage.LOGIN_SUCCESS);
                userinfo =serifs;
                /*登录成功日志*/
                new UserLoginService().insertUserLogins(serifs,ResponseMessage.LOGIN_SUCCESS);
            }else {
                userinfo = new Users();
                /*打印登录失败的消息*/
                System.out.println(ResponseMessage.USER_PASSWORD_ERROR);
                /*密码错误日志*/
                new UserLoginService().insertUserLogins(serifs,ResponseMessage.USER_PASSWORD_ERROR);
            }
        }catch (IndexOutOfBoundsException e) {
            /*打印用户未找到的消息*/
            System.out.println(ResponseMessage.NOT_FOUND_ERROR);
            userinfo = new Users();
        }finally {
            /*关闭链接*/
            sqlSession.close();
        }
        return userinfo;
    }

    /**
     * 用户注册功能实现
     * 调用方法：int userRegister = new UsersService().userRegister(new Users("admin","admin"));
     *
     * @param userRegister 用户的注册参数
     * @return 用户注册是否成功
     */
    public int userRegister(Users userRegister) {
        int sucessfully=0;
        SqlSession sqlSession = MybatisClass.getSqlSession();
        UsersMapper usersMapper = sqlSession.getMapper(UsersMapper.class);
        try {
            sucessfully= usersMapper.insertSelective(userRegister);
        }catch (Exception e) {
            sucessfully= 2;
        }finally {
            sqlSession.commit();
            sqlSession.close();
        }
        return sucessfully;
    }

    /**
     * 登录的返回提示信息处理方法
     * 调用方法：System.out.println(UsersService.getMessage(userRegister));
     *
     * @param userRegisterSuccess 状态信息
     * @return 返回的提示信息
     */
    public static String getMessage(int userRegisterSuccess) {
        if (userRegisterSuccess == 0) {
            return ResponseMessage.SERVER_ERROR;
        }else if(userRegisterSuccess == 1){
            return ResponseMessage.LOGIN_SUCCESS;
        }else if (userRegisterSuccess == 2) {
            return ResponseMessage.REPEAT_ERROR;
        }else{
            return ResponseMessage.ANOTHER_ERROR;
        }
    }

    /**
     * 得到单个用户的用户相信
     * @param userRegister 用户的传递信息，必要传递的值是用户的id，空时候我们显示的是不为管理员的所有用户，当不为空时候，返回id 的list 的用户信息
     * @return 返回得到单个用户的用户相信
     */
    public List<Users> userInfo(List<Integer> userRegister) {
       List<Users> userinfo = null;
        SqlSession sqlSession = MybatisClass.getSqlSession();
        UsersMapper usersMapper = sqlSession.getMapper(UsersMapper.class);
        UsersExample userExample = new UsersExample();
        try {
            if (userRegister.size() <= 0) {
                userExample.createCriteria().andIdIsNotNull().andIsAdminEqualTo(false);
                userinfo=usersMapper.selectByExample(userExample);
            }else{
                userExample.createCriteria().andIdIsNotNull().andIdIn(userRegister);
                userinfo=usersMapper.selectByExample(userExample);
            }
        }catch (Exception e) {
            userinfo=new ArrayList<Users>();
        }finally {
            sqlSession.commit();
            sqlSession.close();
        }
        return userinfo;
    }

}
